package com.jf.springboot.config;

import com.jf.springboot.bean.IdCard;
import com.jf.springboot.interceptor.UserInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-12-05
 * Time:14:41
 */
@Configuration(proxyBeanMethods = false)
public class WebConfig {
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addFormatters(FormatterRegistry registry) {
                registry.addConverter(new Converter<String, IdCard>() {
                    @Override
                    public IdCard convert(String source) {
                        if (!ObjectUtils.isEmpty(source)) {
                            source = source.trim();
                            String[] split = source.split(";");
                            IdCard idCard = new IdCard();
                            idCard.setId(Integer.parseInt(split[0].trim()));
                            idCard.setNumber(split[1].trim());
                            return idCard;
                        }
                        return null;
                    }
                });
            }

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                InterceptorRegistration interceptorRegistration = registry.addInterceptor(new UserInterceptor());
                interceptorRegistration.addPathPatterns("/user/**");
                interceptorRegistration.excludePathPatterns("/user/login","/user/upload");
            }
        };
    }
}
